<!--
Code used for embedding Gumroad on the Hydejack Site. 
Left here for reference, feel free to delete.
-->
<link rel="dns-prefetch" href="https://assets.gumroad.com">
<script>window.GET_CLAPS_API = 'https://worker.getclaps.dev'</script>
<script type="module">
  const ppiData = window._ppiData = fetch('https://hydejack-ppi.qwtel.workers.dev', { mode: 'cors'}).then(res => res.json()).catch(() => ({}));
  const promisify = f => x => new Promise(r => f(x).addEventListener('load', r));
  const loadJS = promisify(window.loadJS);
  {
    let p;
    document.querySelector('hy-push-state').addEventListener('load', () => {
      const io = new IntersectionObserver(async (entries) => {
        if (entries.some(x => x.isIntersecting)) {
          p = p || loadJS('https://gumroad.com/js/gumroad-embed.js');
          const [{ code }] = await Promise.all([ppiData, p]);
          document.querySelectorAll('.gumroad-product-embed').forEach(el => { 
            if (!el.dataset.gumroadParams) {
              el.dataset.gumroadParams = 'offer_code=' + (code || 'qr0tw8m');
            }
          });
          if (!window.GumroadEmbed) {
            await new Promise(function check1(res) {
              if ('createGumroadEmbed' in window) res(window.createGumroadEmbed());
              else setTimeout(() => check1(res), 200);
            });
          }
          await new Promise(function check2(res) {
            if ('GumroadEmbed' in window) res(GumroadEmbed.reload());
            else setTimeout(() => check2(res), 200);
          });
        }
      }, { rootMargin: '1440px' });
      document.querySelectorAll('.gumroad-product-embed').forEach(el => io.observe(el));
    });
  }
  {
    let p;
    document.querySelector('hy-push-state').addEventListener('load', () => {
      const io = new IntersectionObserver(async (entries) => {
        if (entries.some(x => x.isIntersecting)) {
          p = p || loadJS('https://gumroad.com/js/gumroad.js');
          const [{ code }] = await Promise.all([ppiData, p]);
          if (code) {
            document.querySelectorAll('.gumroad-button').forEach(el => { 
              if (!el.href.endsWith(code)) {
                el.href = el.href + '/' + (code || 'qr0tw8m');
              }
            });
          }
          if (!window.GumroadOverlay) {
            await new Promise(function check(res) {
              if ('createGumroadOverlay' in window) res(window.createGumroadOverlay());
              else setTimeout(() => check(res), 200);
            });
          }
        }
      }, { rootMargin: '300px' });
      document.querySelectorAll('.gumroad-button').forEach(el => io.observe(el));
    });
  }
</script>
